
#include <string.h>
#include "BC26.h"
#include "stdio.h"
#include "user_uart_interrupt.h"
#include "TelecomPlatform.h"
#include "main.h"
#include "user_delay.h"
#include "bstim32_timing.h"
extern uint8_t UART0_Rx_Buf[MAX_REC_LENGTH];
extern uint16_t  UART0_Rx_cnt ;
extern uint8_t UART0_temp[REC_LENGTH];

uint8_t BC26_QLWSERV(uint8_t *TelecomPlatformIP,uint8_t*TelecomPlatformPort)
{
	uint8_t i=0,sockedid=0,result=0 ;
	uint8_t ATStrBuf[50]={0};
	const uint8_t Buf1[2]={'"',0 };
	const uint8_t Buf2[3]={'"',',',0};
	SysPrintf("\n--->>>Config IP and Port...\n");
	sprintf(ATStrBuf,"AT+QLWSERV=");
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,TelecomPlatformIP);
	strcat(ATStrBuf,Buf2);
	strcat(ATStrBuf,TelecomPlatformPort);
	strcat(ATStrBuf,"\r\n\0");
	SysPrintf("ATStrBuf=%s\n",ATStrBuf);
	i=3;
	while(i--)
	{
		UART0_Rx_cnt=0 ;
		memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		
		UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART0_Rx_Buf);
		if((strstr(UART0_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("BC26_QLWSERV success\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	SysPrintf("BC26_QLWSERV fail\n");
	return FALSE;
}

uint8_t BC26_QLWCONF(uint8_t *IMEI)
{
	uint8_t i=0,sockedid=0,result=0 ;
	uint8_t ATStrBuf[50]={0};
	const uint8_t Buf1[2]={'"',0 };
	SysPrintf("\n--->>>Config Equipment IMEI\n");
	sprintf(ATStrBuf,"AT+QLWCONF=");
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,IMEI);
	strcat(ATStrBuf,Buf1);
	strcat(ATStrBuf,"\r\n\0");
	SysPrintf("ATStrBuf=%s\n",ATStrBuf);
	i=3;
	while(i--)
	{
		UART0_Rx_cnt=0 ;
		memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		
		UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART0_Rx_Buf);
		if((strstr(UART0_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("BC26_QLWCONF success\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	SysPrintf("BC26_QLWCONF fail\n");
	return FALSE;
}

uint8_t BC26_QLWADDOBJ(void)
{
	uint8_t i=0,sockedid=0,result=0;
	
	uint8_t  ATStrBuf_UPchannle[25+1]={'A','T','+','Q','L','W','A','D','D','O','B',\
		                             'J','=','1','9',',','0',',','1',',','"','0','"','\r','\n',0};
	uint8_t  ATStrBuf_Downchannle[25+1]={'A','T','+','Q','L','W','A','D','D','O','B',\
		                             'J','=','1','9',',','1',',','1',',','"','0','"','\r','\n',0};
	SysPrintf("\n--->>>add LwM2M obj\n");
	i=3;
	while(i)
	{
	    --i;
		UART0_Rx_cnt=0 ;
		memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf_UPchannle,strlen(ATStrBuf_UPchannle));
		HAL_Delay(1000);
		
		UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART0_Rx_Buf);
		if((strstr(UART0_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("BC26_QLWADDOBJ up success\n");
			
			break;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	if(i==0)
	{
		SysPrintf("BC26_QLWADDOBJ up fail\n");
	}
	
	i=3;
	while(i)
	{
		--i;
		UART0_Rx_cnt=0 ;
		memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf_Downchannle,strlen(ATStrBuf_Downchannle));
		HAL_Delay(1000);
		
		UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART0_Rx_Buf);
		if((strstr(UART0_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("BC26_QLWADDOBJ down success\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	SysPrintf("BC26_QLWADDOBJ down fail\n");
	return  FALSE;
}

uint8_t BC26_QLWOPEN(void)
{
	uint8_t i=0,sockedid=0,result=0,j=0;
	uint8_t  ATStrBuf[14+1]={'A','T','+','Q','L','W','O','P','E','N','=','1','\r','\n',0};
	SysPrintf("\n--->>>Send Registration request\n");
	i=2;
	while(i)
	{
		--i;
		UART0_Rx_cnt=0 ;
		memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		
		UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
		
		if((strstr(UART0_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("BC26 Regist  success\n");
			
			if(strstr(UART0_Rx_Buf,"CONNECT OK")==NULL)
			{
				j=0;
				UART0_Rx_cnt=0 ;
				memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
				while(1)
				{
					HAL_Delay(1000);
					SysPrintf("*");
					if(++j>128)
					{
						break;
					}
					if(UART0_Rx_cnt>0)
					{
						HAL_Delay(1000);
						
						UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
						SysPrintf("%s\n",UART0_Rx_Buf);
						if(strstr(UART0_Rx_Buf,"CONNECT OK")!=NULL)
						{
								break;
						}
						else if(strstr(UART0_Rx_Buf,"CONNECT FAIL")!=NULL)
						{
								return  FALSE;
						}
						else
						{
							UART0_Rx_cnt=0 ;
							memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
						}
					}
				}
			}
			j=0;
			UART0_Rx_cnt=0 ;
			memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
			while(1)
			{
				HAL_Delay(1000);
				SysPrintf("*");
				if(++j>60)
				{
					break;
				}
				if(UART0_Rx_cnt>0)
				{
					HAL_Delay(1000);
					SysPrintf("--len=%d\n",UART0_Rx_cnt);
					UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
					SysPrintf("%s\n",UART0_Rx_Buf);
					if(strstr(UART0_Rx_Buf,"+QLWOBSERVE:")!=NULL)
					{
						return TRUE;
					}
					else
					{
						UART0_Rx_cnt=0 ;
						memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
					}
				}
			}
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	SysPrintf("BC26 Regist fail\n");
	return  FALSE;
}

uint8_t BC26_QLWUPDATE(void)
{
	uint8_t i=0,sockedid=0,result=0,j=0;
	uint8_t  ATStrBuf[14+1]={'A','T','+','Q','L','W','U','P','D','A','T','E','\r','\n',0};
	SysPrintf("\n--->>>Send update request\n");
	i=2;
	while(i--)
	{
		UART0_Rx_cnt=0 ;
		memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(500);
		
		UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
		
		if((strstr(UART0_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("QLWUPDATE success\n");
			if(strstr(UART0_Rx_Buf,"UPDATE OK")==NULL)
			{
				j=0;
				UART0_Rx_cnt=0 ;
				memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
				
				while(1)
				{
					HAL_Delay(1000);
					SysPrintf("*");
					if(++j>60)
					{
						break;
					}
					if(UART0_Rx_cnt>0)
					{
						HAL_Delay(1000);
						
						UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
						SysPrintf("%s\n",UART0_Rx_Buf);
						if(strstr(UART0_Rx_Buf,"UPDATE OK")!=NULL)
						{
							SysPrintf("update success\n");
							return TRUE;
						}
						else	if(strstr(UART0_Rx_Buf,"UPDATE FAIL")!=NULL)
						{
							SysPrintf("update fail\n");
							return FALSE;
						}
					}
				}
			}
			else
			{
				SysPrintf("Update Success\n");
				return TRUE;
			}
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	SysPrintf("BC26_QLWUPDATE fail\n");
	return FALSE;
}

uint8_t BC26_QLWCFG(void)
{
	uint8_t i=0,sockedid=0,result=0 ;
	
	uint8_t  ATStrBuf[28+1]={'A','T','+','Q','L','W','C','F','G','=','"','d','a',\
	't','a','f','o','r','m','a','t','"',',','0',',','0','\r','\n',0};
	SysPrintf("\n--->>>Config dataformat\n");
	i=3;
	while(i--)
	{
		UART0_Rx_cnt=0 ;
		memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(1000);
		
		UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART0_Rx_Buf);
		if((strstr(UART0_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("AT+QLWCFGsuccess\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	SysPrintf("AT+QLWCFG fail\n");
	return FALSE;
}

uint16_t SendDataToTelecomPlatform(uint8_t*buffer,uint16_t len)
{
    uint8_t i=3,j=0;
    uint8_t ATStrBuf[150]={0};
    sprintf(ATStrBuf,"AT+QLWDATASEND=19,0,0,%d,%s,0x0100\r\n",len,buffer);
    while(i--)
    {
    	
        UART0_Rx_cnt=0;
		memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
        SendDataToNBModule(ATStrBuf,strlen(ATStrBuf)+1);
			SysPrintf(">>>>>>>>%s\r\n",ATStrBuf);
        HAL_Delay(500);
        
        UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
		
        if((strstr(UART0_Rx_Buf,"OK")!=NULL))
        
       
        {
        	
			if(strstr(UART0_Rx_Buf,"SEND OK")==NULL)
			{
				UART0_Rx_cnt=0 ;
				memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
				j=30;
				while(j--)
				{
					SysPrintf("*");
					HAL_Delay(1000);
					if(UART0_Rx_cnt>0)
					{
						
						UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
						
						if(strstr(UART0_Rx_Buf,"SEND OK")!=NULL)
						{
							 SysPrintf("send  data success\n");
							 return	TRUE;
						}
						else if(strstr(UART0_Rx_Buf,"SEND FAIL")!=NULL)
						{
							SysPrintf("send  data fail\n");
							return FALSE;
						}
					}
				}
			}
			else
			{
				SysPrintf("Send  Data Success\n");
				return	TRUE;
			}
        }
        else
        {
			
            SysPrintf("---->>>Send  ERR\n");
			
			
			HAL_Delay(2000);
        }
    }
    return FALSE ;
}

uint16_t	ReceiveMessageFromTelecomPlatform(uint8_t*recebuffer)
{
	uint8_t i=0,sockedid=0,len=0 ;
	uint8_t  ATStrBuf[14+1]={'A','T','+','Q','L','W','R','D','=','1','0','0','\r','\n',0};
	uint8_t *start;
	SysPrintf("\n--->>>Receive Data from Telecom\n");
	
	i=6;
	while(i--)
	{
		UART0_Rx_cnt=0 ;
		memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(700);
		LED_blink(2);
		if((strncmp(UART0_Rx_Buf,"AT+QLWRD=100",strlen("AT+QLWRD=100"))==0)&&\
		(strstr(UART0_Rx_Buf,"OK")!=NULL))
		{
			start = strstr(UART0_Rx_Buf, "+QLWRD:");
			if (start != NULL)
			{
				len=UtilGetIntFlag(start,"+QLWRD:",",");
				if(len>0)
				{
					
					start = strstr(UART0_Rx_Buf, ",");
					if (start != NULL)
					{
						start = strstr(start, "\r\n");
						if (start != NULL)
						{
							memcpy(recebuffer,start+strlen("\r\n"),len);
							recebuffer[len]=0;
							SysPrintf("data=%s\n",recebuffer);
							return len;
						}
					}
				}
			}
		}
	}
	SysPrintf("NO data to receive\n");
	return 0;
}

uint8_t BC26_QLWDEL(void)
{
	uint8_t i=0,sockedid=0,result=0;
	
	uint8_t  ATStrBuf[11+1]={'A','T','+','Q','L','W','D','E','L','\r','\n'};
	SysPrintf("\n--->>>ɾ�� LwM2M ����\n");
	i=3;
	while(i--)
	{
		UART0_Rx_cnt=0 ;
		memset(UART0_Rx_Buf,0,Uart_ReceBufLen);
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
		HAL_Delay(2000);
		
		UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
		SysPrintf("%s\n",UART0_Rx_Buf);
		if((strstr(UART0_Rx_Buf,"OK")!=NULL))
		{
			SysPrintf("AT+QLWDEL success\n");
			return TRUE;
		}
		else if((strstr(UART0_Rx_Buf,"ERROR")!=NULL))
		{
			SysPrintf("AT+QLWDEL fail\n");
			return FALSE;
		}
		else
		{
			HAL_Delay(2000);
		}
	}
	SysPrintf("AT+QLWDEL fail\n");
	return FALSE;
}